package com.linesum.code.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import com.linesum.code.commons.GlobalConstant;
import com.linesum.code.commons.ResultResponse;

/**
 * 登录Controller
 * @author chenzf
 * @date 2017年7月26日
 */
@RestController
@SuppressWarnings("static-method")
public class LoginController {

	@PostMapping("/login")
	public ResultResponse login(HttpServletRequest request) {
		WebUtils.setSessionAttribute(request, GlobalConstant.SESSION_KEY_USERNAME, "123");
		return new ResultResponse(true);
	}

	@PostMapping("/logout")
	public ResultResponse logout(HttpServletRequest request) {
		Object username = WebUtils.getSessionAttribute(request, GlobalConstant.SESSION_KEY_USERNAME);
		if (username == null) {
			return new ResultResponse(false, "获取session对象失败");
		}
		//request.getSession().removeAttribute(GlobalConstant.SESSION_KEY_USERNAME);
		return new ResultResponse(true,
				StringUtils.join(GlobalConstant.SESSION_KEY_USERNAME, "-", username.toString()));
	}
}
